// by Xeno
//#define __DEBUG__
#define THIS_FILE "fn_playerspawn.sqf"
#include "x_setup.sqf"
#define __prma _p removeAction _id
#define __addbp __pSetVar [#d_pbp_id, _p addAction [_s call FUNC(GreyText), {_this spawn FUNC(backpack)},[],-1,false,true,"","!surfaceIsWater (getPosASL _target) && {vehicle _target == _target} && {(getPos _target) select 2 < 2} && {!(_target getVariable 'xr_pluncon')}"]]

private ["_rtype", "_p", "_oabackpackmags", "_oabackpackweaps", "_ubp", "_ubackp", "_hasruck", "_ruckmags", "_ruckweapons", "_backwep", "_ident", "_id", "_types", "_type", "_ar", "_hh", "_primw", "_muzzles", "_bp", "_mags", "_mcount", "_i", "_weaps", "_s", "_action"];
PARAMS_1(_rtype);
__TRACE_1("","_rtype");

if (_rtype == 0) then { // player died
	if (visibleMap) then {
		openMap false;
	};
	if (GVAR(WithRevive) == 1) then {
		setPlayerRespawnTime GVAR(RespawnTime);
	};
	// TYRGHEN - was removed by Xeno, but how do __pSetVar then work???
	_p = player;
	if (GVAR(WithRevive) == 1) then {
		__pSetVar [QGVAR(is_leader), if (player == leader (group player)) then {group player} else {objNull}];
	};
	__TRACE("removing handleDamage eventhandlers");
	player removeAllEventHandlers "handleDamage";

	if (GVAR(WithBackpack)) then {
		_id = __pGetVar(GVAR(pbp_id));
		if (_id != -9999) then {
			__prma;
			__pSetVar [QGVAR(pbp_id), -9999];
		};
	};
	private "_trencho";
	_trencho = __pGetVar(GVAR(trench));
	if (!isNil "_trencho" && {!isNull _trencho}) then {
		deleteVehicle _trencho;
		__pSetVar [QGVAR(trench), objNull];
	};
} else { // _rtype = 1, player has respawned
	__pSetVar [QGVAR(alivetimestart), time];
	GVAR(commandingMenuIniting) = false;
	
	if (GVAR(WithRevive) == 1) then {
		__TRACE("adding no revive handleDamage eventhandler");
		player addEventHandler ["handleDamage", {_this call FUNC(playerHD)}];
	} else {
		__TRACE("adding revive handleDamage eventhandler");
		player addEventHandler ["handleDamage", {_this call xr_fnc_ClientHD}];
	};
	/*0 spawn {
		_timeend = time + 2;
		waitUntil {!BIS_respawnInProgress || {time > _timeend}};
		sleep 0.2;
		__TRACE("respawn removing all handleDamage eventhandlers");
		player removeAllEventHandlers "handleDamage";
		if (GVAR(WithRevive) == 1) then {
			__TRACE("adding no revive handleDamage eventhandler");
			player addEventHandler ["handleDamage", {_this call FUNC(playerHD)}];
		} else {
			__TRACE("adding revive handleDamage eventhandler");
			player addEventHandler ["handleDamage", {_this call xr_fnc_ClientHD}];
		};
	};*/
	xr_phd_invulnerable = true;
	player setFatigue 0;
	_p = player;
	if (GVAR(weapon_respawn)) then {
		//removeAllWeapons _p;
		//removeAllItems _p;
		//removeBackpack _p;
		#define __addmx _p addMagazine _x
		#define __addwx _p addWeapon _x
		if (GVAR(WithRevive) == 1 && {count GVAR(custom_layout) > 0}) then {
			call FUNC(retrieve_layoutgear);
			if (GVAR(WithBackpack) && {count __pGetVar(GVAR(custom_backpack)) > 0}) then {
				__pSetVar [QGVAR(player_backpack), __pGetVar(GVAR(custom_backpack))];
			};
		} else {
			call FUNC(retrieve_respawngear);
			if (count GVAR(backpack_helper) > 0) then {
				{__addmx} forEach (GVAR(backpack_helper) select 3);
				_p addWeapon (GVAR(backpack_helper) select 1);
				{__addmx} forEach (GVAR(backpack_helper) select 1);
				{if (_x != "") then {_p removePrimaryWeaponItem _x}} forEach (primaryWeaponItems _p);
				{if (_x != "") then {_p addPrimaryWeaponItem _x}} foreach (GVAR(backpack_helper) select 2);
				GVAR(backpack_helper) = [];
			};
		};
	};
	"RadialBlur" ppEffectAdjust [0.0, 0.0, 0.0, 0.0];
	"RadialBlur" ppEffectCommit 0;
	"RadialBlur" ppEffectEnable false;

	if ((GVAR(with_ai) || {GVAR(with_ai_features) == 0}) && {rating _p < 20000}) then {_p addRating 20000};
	if (GVAR(WithBackpack)) then {
		if (count __pGetVar(GVAR(player_backpack)) == 0) then {
			if (primaryWeapon _p != "") then {
				_s = format [(localize "STR_DOM_MISSIONSTRING_155"), [primaryWeapon _p,1] call FUNC(GetDisplayName)];
				if (__pGetVar(GVAR(pbp_id)) == -9999) then {__addbp};
			};
		} else {
			_s = format [(localize "STR_DOM_MISSIONSTRING_154"), [__pGetVar(GVAR(player_backpack)) select 0,1] call FUNC(GetDisplayName)];
			if (__pGetVar(GVAR(pbp_id)) == -9999) then {__addbp};
		};
	};
	if (!isNil {__pGetVar(GVAR(bike_created))}) then {__pSetVar [QGVAR(bike_created), false]};
	
	if (GVAR(WithRevive) == 1) then {
		deleteVehicle ((_this select 1) select 1);
	};
	
	if (sunOrMoon == 0 && {_p call FUNC(hasnvgoggles)}) then {_p action ["NVGoggles",_p]};
	if !(_p getVariable ["xr_isdead", false]) then {
		0 spawn {
			scriptName "spawn_playerspawn_vul";
			waitUntil {!dialog};
			sleep 5;
			xr_phd_invulnerable = false;
		};
	};
	if (GVAR(WithRevive) == 1 && {!isNull __pGetVar(GVAR(is_leader))}) then {
		[QGVAR(grpl), [__pGetVar(GVAR(is_leader)), player]] call FUNC(NetCallEventSTO);
	};
	_clattachedobj = player getVariable [QGVAR(p_clattachedobj), objNull];
	if (!isNull _clattachedobj) then {
		_clattachedobj attachTo [player, [0,-0.03,0.07], "LeftShoulder"]; 
	};
};